import os
import json


def check_and_create_directory(directory_path):
    """检查并创建目录"""
    if not os.path.exists(directory_path):
        os.makedirs(directory_path)
        return True
    return False


def check_and_create_config_file(file_path, default_content):
    """检查并创建配置文件"""
    if not os.path.exists(file_path):
        with open(file_path, 'w', encoding='utf-8') as f:
            json.dump(default_content, f, ensure_ascii=False, indent=4)
        return True
    return False


def get_default_config_content():
    """获取默认的配置内容"""
    return {
        "Model": {
            "model_name": "pixtral-large-latest",
            "api_key": "your_mistral_large_api_key",
            "model_url": "",
            "prompt": "- Role: PowerShell scripting expert and command line assistant\n- Background: Users need "
                      "assistance when working with the Windows operating system, particularly in PowerShell "
                      "scripting and command line operations.\n- Profile: You are an expert familiar with the Windows "
                      "operating system and PowerShell, capable of providing concise and efficient solutions.\n- "
                      "Skills: You possess profound skills in PowerShell script writing and command line operations, "
                      "enabling you to quickly understand and execute user needs.\n- Goals: To provide a PowerShell "
                      "command or solution that meets the specific needs of users in the Windows environment, "
                      "including reading files with UTF-8 encoding.\n- Constrains: Must use Markdown code block "
                      "syntax to display code, YAML, JSON, or XML, excluding tables. For PowerShell commands, "
                      "use ```powershell to encapsulate. Command line commands must be fully displayed on the same "
                      "line, without line breaks. Responses should be concise, without additional commands, "
                      "providing only one command. For questions not executing commands, respond using ```powershell "
                      "echo`. Include UTF-8 when reading files.\n- OutputFormat: Depending on user needs, the output "
                      "format may be a PowerShell command, an explanation, or a simple echo command.\n- Workflow:\n  "
                      "1. Understand the user's needs and context.\n  2. Provide a PowerShell command or solution "
                      "based on the needs, ensuring UTF-8 encoding is used when reading files.\n  3. Correctly "
                      "encapsulate the command or output using Markdown code block syntax.\n  4. "
                      "文件里写入多行内容的时候以powershell脚本来写入。\n 5. 需要打开MySQL，Redis，Python，其他类似于用于命令行程序的程序的时候，在新的终端上打开。"
                      "\n- Examples:\n  - Example 1: The user needs to list all files "
                      "and folders in the current directory.\n    ```powershell\n    Get-ChildItem\n    ```\n  - "
                      "Example 2: The user wants to know how to create a new folder.\n    ```powershell\n    New-Item "
                      "-ItemType Directory -Name \"NewFolder\"\n    ```\n  - Example 3: The user needs to read a file "
                      "with UTF-8 encoding.\n    ```powershell\n    Get-Content -Path \"filename.txt\" -Encoding "
                      "UTF8\n    ```\n  - Example 4: When the user asks a question that requires an answer.\n    "
                      "```powershell\n    echo <answer content>\n    ```\n"
        },
        "Terminal": {
            "terminal_font": "slant",
            "terminal_color": "cyan",
            "terminal_renderText": "AI Shell"
        }
    }


def get_default_model_content():
    """获取默认的模型内容"""
    return {
        "Openai": [
            "gpt3",
            "gpt4",
            "moonshot-v1-8k"
        ],
        "GLM": [
            "glm2",
            "glm3",
            "glm4",
            "glm-4-flash"
        ],
        "LLM": [
            "llm2",
            "llm3",
            "llm4"
        ],
        "mistral": [
            "pixtral-large-latest",
            "pixtral-medium-latest",
            "pixtral-small-latest"
        ]
    }


def ExamineConfig(url):
    """检查配置文件"""
    config_dir = os.path.join(url, 'Config')
    # print(config_dir)
    config_file = os.path.join(config_dir, 'config.json')
    model_file = os.path.join(config_dir, 'model.json')

    # 检查并创建目录
    check_and_create_directory(config_dir)

    # 检查并创建配置文件
    check_and_create_config_file(config_file, get_default_config_content())

    # 检查并创建模型文件
    check_and_create_config_file(model_file, get_default_model_content())

    # 检查配置文件和模型文件是否存在
    if not os.path.exists(config_file):
        check_and_create_config_file(config_file, get_default_config_content())
    elif not os.path.exists(model_file):
        check_and_create_config_file(model_file, get_default_model_content())
    else:
        return True
